<template>
  <div class="process-advance-set-wrap">
    <div class="legal-process-create">
      <div style="align-items: center;display: flex; justify-content: space-between;">
        <div>
          <Title :title="title" />
        </div>
      </div>
      <el-form v-if="!isPreview" class="form-box" label-width="100px" label-position="right">
        <el-form-item label="自动去重设置">
          <el-select
            v-model="paramsObj.autoRemoveRepet"
            placeholder="请选择"
          >
            <el-option
              v-for="item in removeRepetOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item class="check-form-item">
          <el-checkbox v-model="paramsObj.isAutoPass" :true-label="1" :false-label="0">
            <span style="color:#666666;font-size:14px;">审批中发起人自动通过</span>
          </el-checkbox>
        </el-form-item>
        <el-form-item label="加签人设置">
          <el-select
            v-model="paramsObj.countersignSet"
            placeholder="请选择"
          >
            <el-option
              v-for="item in countersignSetOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发起审批说明">
          <el-input
            v-model="paramsObj.explanation"
            resize="none"
            maxlength="500"
            clearable
            class="explanation"
            type="textarea"
            placeholder="审批说明将展示在创建审批页面"/>
        </el-form-item>
        <el-form-item label="审批说明样式">
          <styleConfig ref="styConfig" :new-config="paramsObj.styConfig"/>
        </el-form-item>
      </el-form>
      <el-form v-else class="detail-box" label-width="100px" label-position="right">
        <el-form-item label="自动去重设置:">
          <span>{{ paramsObj.autoRemoveRepet | mapOptions(removeRepetOption) }}</span>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="paramsObj.isAutoPass" :disabled="true" :true-label="1" :false-label="0">
            <span style="color:#666666;font-size:14px;">审批中发起人自动通过</span>
          </el-checkbox>
        </el-form-item>
        <el-form-item label="加签人设置:">
          <span>{{ paramsObj.countersignSet | mapOptions(countersignSetOption) }}</span>
        </el-form-item>
        <el-form-item label="发起审批说明:">
          <el-input
            v-model="paramsObj.explanation"
            maxlength="500"
            class="explanation"
            disabled
            resize="none"
            type="textarea"
          />
        </el-form-item>
        <el-form-item label="审批说明样式">
          <styleConfig ref="styConfig" :disabled="true" :new-config="paramsObj.styConfig"/>
        </el-form-item>
      </el-form>
    </div>
    <fixed-pagination>
      <div v-if="!isPreview" class="center">
        <el-button :loading="opeatorLoading" type="primary" class="submit-btn" size="small" @click="toSubmit">保存</el-button>
        <el-button type="info" class="back-btn cancel-btn-style" size="small" @click="toBack">取消</el-button>
      </div>
      <div v-else class="center">
        <el-button :loading="opeatorLoading" type="primary" size="small" class="submit-btn" @click="toEdit">编辑</el-button>
        <el-button type="info" class="back-btn cancel-btn-style" size="small" @click="toBack">取消</el-button>
      </div>
    </fixed-pagination>
  </div>
</template>
<script>
import { getAdvancedSet, setAdvancedSet } from '@/api/legal';
import ElMessage from '@/utils/message';
import Title from '@/components/Title';
import styleConfig from './styleConfig';
import { mapOptions } from '@/filters/index';
export default {
  name: 'ProcessAdvanceSet',
  filters: {
    mapOptions
  },
  components: {
    Title,
    styleConfig
  },
  props: {
    isPreview: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      title: '',
      processId: '',
      isEdit: false,
      opeatorLoading: false,
      removeRepetOption: [{
        value: 1,
        label: '同一个审批人仅在连续出现时，自动去重'
      }, {
        value: 0,
        label: '不自动去重'
      }],
      countersignSetOption: [{
        value: 1,
        label: '添加加签人不限制'
      }, {
        value: 2,
        label: '申请人不允许加签'
      }, {
        value: 3,
        label: '审批人不允许加签'
      }],
      paramsObj: {
        isAutoPass: 0,
        autoRemoveRepet: '',
        explanation: '',
        styConfig: '',
        countersignSet: 1
      }
    };
  },
  created() {
    this.processId = Number(this.$route.params.id);
    if (this.processId) {
      this.getData();
      this.isEdit = true;
    }
  },
  methods: {
    getData() {
      getAdvancedSet(this.processId).then(res => {
        if (res.code === 200) {
          const data = res.data;
          this.title = data.name;
          this.paramsObj.isAutoPass = data.isAutoPass || 0;
          this.paramsObj.autoRemoveRepet = data.autoRemoveRepet;
          this.paramsObj.countersignSet = data.countersignSet || 1;
          this.paramsObj.explanation = data.explanation || '';
          this.paramsObj.styConfig = data.styConfig;
        }
      });
    },
    toSubmit() {
      if (this.opeatorLoading) return;
      this.opeatorLoading = true;
      this.paramsObj.styConfig = this.$refs['styConfig'].getstyConfig();
      setAdvancedSet(this.processId, this.paramsObj).then(res => {
        this.opeatorLoading = false;
        if (res.code === 200) {
          ElMessage.success('高级设置保存成功');
          this.getData();
        }
      }).catch((_) => {
        this.opeatorLoading = false;
        ElMessage.success('高级设置失败');
      });
    },
    toEdit() {
      this.$router.push({
        name: 'ContractProcessEdit',
        params: { id: this.processId },
        query: {
          activeId: 1
        }
      });
    },
    toBack() {
      this.$router.push({ name: 'ContractProcess' });
    }
  }
};
</script>
<style lang="scss" scoped>
.explanation{
  width: 300px;
}
.detail-box {
  & ::v-deep {
    .el-form-item {
      height: auto;
      line-height: unset;
      margin-bottom: 16px;
      .el-form-item__label {
        height: auto;
        line-height: unset;
        color: #999;
        // text-align: left;
      }
      .el-form-item__content {
        height: auto;
        line-height: unset;
      }
    }
  }
}
.form-box, .detail-box {
  margin-top: 16px;
  & ::v-deep {
    .el-input__inner {
      width: 240px;
      height: 32px;
      line-height: 32px;
    }
    .el-textarea .el-textarea__inner{
      width: 610px;
      height: 120px;
    }
    .el-input__icon {
      line-height: 32px;
      height: 32px;
    }
  }
}
.form-box {
  & ::v-deep {
    .el-form-item {
      margin-bottom: 24px;
    }
  }
}
.check-form-item {
  margin-bottom: 22px !important;
  & ::v-deep {
    .el-form-item__content {
      line-height: 1;
    }
  }
}
.legal-process-create {
  margin: 16px;
  background-color: #fff;
  padding: 24px;
  min-height: calc(100vh - 80px);
}
.create-process-btn {
  display: flex;
  align-items: center;
  margin: 20px;
  .submit-btn {
    width: 120px;
    border-radius: 4px;
    font-size: 14px;
    font-weight: 500;
    color: #fff;
    line-height: 16px;
    padding: 7px 0;

  }
  .back-btn {
    width: 80px;
    border-radius: 4px;
    font-size: 14px;
    font-weight: 500;
    padding: 7px 0;
    background-color: #EFF3FB;
    color: #406EFF;
    line-height: 16px;
    border-color: #EFF3FB;
  }
}
</style>
